package com.company.project.rocketmq.processor;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.company.project.constant.TradeEnums;
import com.company.project.protocol.coupon.ChangeCoponStatusReq;
import com.company.project.protocol.mq.CancelOrderMQ;
import com.company.project.rocketmq.IMessageProcessor;
import com.company.project.service.TradeCouponService;

/**
 * 优惠券模块 处理 取消订单的 处理逻辑 一般指定 一个 groupName : user_ace_trade_cancel_group
 */
@Component
public class CouponCancelOrderProcessor implements IMessageProcessor {
	public static final Logger LOGGER = LoggerFactory.getLogger(CouponCancelOrderProcessor.class);
	@Autowired
	private TradeCouponService tradeCouponService;

	public boolean handleMessage(MessageExt messageExt) {
		try {
			String body = new String(messageExt.getBody(), "UTF-8");
			String msgId = messageExt.getMsgId();
			String tags = messageExt.getTags();
			String keys = messageExt.getKeys();
			LOGGER.info("CouponCancelOrderProcessor receive message:" + body);
			CancelOrderMQ cancelOrderMQ = JSON.parseObject(body, CancelOrderMQ.class);

			if (StringUtils.isNotBlank(cancelOrderMQ.getCouponId())) {
				ChangeCoponStatusReq changeCoponStatusReq = new ChangeCoponStatusReq();
				changeCoponStatusReq.setOrderId(cancelOrderMQ.getOrderId());
				changeCoponStatusReq.setCouponId(cancelOrderMQ.getCouponId());
				changeCoponStatusReq.setIsUsed(TradeEnums.YesNoEnum.NO.getCode());
				this.tradeCouponService.changeCoponStatus(changeCoponStatusReq);
			}
			return true;
		} catch (Exception e) {
			return false;
		}

	}

}
